package com.jflm.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

import cat.util.charset.Base64;

/**
 * ******************  ��˵��  *********************
 * class       :  HmacSHAUtil
 * @author     :  hejinyun@umpay.com
 * @version    :  1.0  
 * description :  
 * @see        :                        
 * ***********************************************
 */
public class HmacSHAUtil {
	
	private static final Logger log = Logger.getLogger(HmacSHAUtil.class);
	
	/**
	 * ********************************************
	 * method name   : hmacSha1 
	 * description   : HmacSHA1ǩ��,Ĭ��ƽ̨�ַ���
	 * @return       : String
	 * @param        : @param source
	 * @param        : @param key
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2015-7-27  ����3:33:58
	 * @see          : 
	 * *******************************************
	 */
	public static String hmacSha1(String source, String key) {
		try {
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
			Mac mac = Mac.getInstance("HmacSHA1");
			mac.init(keySpec);
			byte[] result = mac.doFinal(source.getBytes());
			return new String(Base64.encode(result));
		} catch (Exception e){
			log.error("#hmacSha1() ����HmacSHA1ǩ���쳣:"+e.toString(), e);
			return "";
		}
	}
	
	/**
	 * ********************************************
	 * method name   : hmacSha1 
	 * description   : HmacSHA1ǩ��,�Զ����ַ���
	 * @return       : String
	 * @param        : @param source
	 * @param        : @param key
	 * @param        : @param encode
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2015-8-7  ����2:53:21
	 * @see          : 
	 * *******************************************
	 */
	public static String hmacSha1(String source, String key, String encode){
		try {
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
			Mac mac = Mac.getInstance("HmacSHA1");
			mac.init(keySpec);
			byte[] result = mac.doFinal(source.getBytes((null == encode)?"GBK":encode));
			return new String(Base64.encode(result));
		} catch (Exception e){
			log.error("#hmacSha1() ����HmacSHA1ǩ���쳣:"+e.toString(), e);
			return "";
		}
	}
}
